<template>
  <div v-if="!loading">
    <!-- 歌单 -->
    <div class="playlist" :style="`background-image: url('${playlist.coverImgUrl}');`">
      <!-- 歌单主体 -->
      <div class="playlistMain">
        <!-- 歌单图片 -->
        <div class="playlistImg">
          <img :src="playlist.coverImgUrl" alt="">
          <span>歌单</span>
          <i class="">{{ playlist.playCount | formatCount }}</i>
        </div>

        <!-- 歌单信息 -->
        <div class="playlistInfo">
          <!-- 歌单名称 -->
          <h3>{{ playlist.name }}</h3>

          <!-- 歌单创建者 -->
          <a href="javascript:;" class="playlistInfoAuthor">
            <div class="playlistInfoImg">
              <img :src="playlist.creator.avatarUrl" alt="">
              <i class=""></i>
            </div>
            <span>{{ playlist.creator.nickname }}</span>
          </a>

        </div>
      </div>
    </div>

    <!-- 歌单歌曲列表标题 -->
    <div class="playlistSongsTitle">歌曲列表</div>

    <!-- 歌单歌曲列表 -->
    <div class="playlistSongs">

      <!-- 每一项歌曲 -->
      <router-link class="playlistSongsItem" v-for="(item, i) in musicList" :key="item.id" :to="`/song/${item.id}`" tag="div">
        <!-- 歌曲序列 -->
        <div class="playlistSongsItemSerial">{{ i+1 }}</div>

        <!-- 歌曲信息 -->
        <div class="playlistSongsItemMusic">
          <!-- 歌名 -->
          <div class="musicName rowShows">{{item.name}}</div>
          <!-- 歌手 -->
          <div class="musicSinger rowShows">
            <span v-for="(ar,i) in item.ar" :key="i">
              {{ ar.name }}
              <i v-if="item.ar.length == 1"></i>
              <i v-else-if="item.ar.length-1 == i"></i>
              <i v-else>/</i>
            </span>
          </div>
        </div>

        <!-- 播放按钮 -->
        <div class="play">
          <i></i>
        </div>

      </router-link>

    </div>

    <!-- 查看更多 -->
    <!-- 1. userId 为 1, 好像是榜单 -->
    <!--   榜单: 直接可以获取到100首歌曲,且没有后续歌曲,不需要查看更多 -->
    <!-- 2. 普通歌单,只能获取到20首歌曲,所以需要查看更多歌曲 -->
    <!--   但是,如果歌单的歌曲不足20首,也是不需要查看更多的 -->
    <!-- 3. 歌单的歌曲加载完,也是不需要显示加载更多的 -->
    <div class="viewMore" v-if="!(playlist.userId === 1) && !(playlist.tracks.length < 20)" v-show="viewMore">
      <span @click="getMusic(listStart)">查看更多</span>
    </div>
  </div>

  <Loading v-else></Loading>
</template>

<script>
import Loading from '@/views/loading/Loading.vue'

export default {
  name: "More",
  data () {
    return {
      // 页面加载
      loading: true,
      // 歌单
      playlist: {},
      // 音乐列表
      musicList: [],
      // 歌单列表偏移量
      listStart: 20,
      // 加载更多歌曲
      viewMore: true,
      playlistCount: ''
    }
  },
  components: {
    Loading
  },
  filters: {
    // 播放次数
    formatCount (count) {
      let str = ''
      console.log(count);

      if (count < 10000) {
        str = count + '次'
      }
      else if (count < 100000000) {
        str = (count / 10000).toFixed(1) + '万'
      }
      else if (100000000 <= count) {
        str = (count / 100000000).toFixed(1) + '亿'
      }
      else {
        str = '0次'
      }

      return str
    }
  },
  computed: {
    getCount () {
      if (this.playlistCount < 1000) {
        return this.playlistCount
      }
      return 1000
    }
  },
  mounted () {
    // 在获取数据时,不加载页面
    this.loading = true

    // 获取当前歌单的详细信息
    this.$api.getPlaylistDetails(this.$route.params.id).then(res => {
      // 歌单信息
      this.playlist = res.playlist
      // 歌曲列表
      this.musicList = res.playlist.tracks
      // 获取数据完毕后,加载页面
      this.loading = false

      // 保存歌单的总数量
      this.playlistCount = res.playlist.trackCount
    })
  },
  methods: {
    getMusic (listStart) {
      // 判断歌单的总歌曲长度 是否小于等于 当前歌曲列表的长度
      if (this.playlist.trackIds.length <= this.musicList.length) {
        // 歌曲全部获取到了, 就直接返回, 并且禁用按钮
        this.viewMore = false
        return ''
      }

      // 获取剩余歌曲 ( 每次50首 )
      this.$api.getMusicList(this.playlist.id, this.getCount - 20, listStart).then(res => {
        if (res.code === 200) {
          this.musicList.push(...res.songs)
          this.listStart += this.getCount - 20

          console.log(res);

          // 判断获取到的总歌曲数量有没有达到歌单的总共歌曲数量
          // 如果相等了，说明达到了。就不需要显示加载更多了
          // --- 优化：在加载结束之前禁止点击多次，在加载过程中可以显示加载中等提示 ---
          if (this.musicList.length == this.playlistCount) {
            this.viewMore = false
          }
        }
      })
    }
  }
}
</script>

<style lang='less' scoped>
// 歌单的主体信息盒子
.playlist {
  position: relative;
  width: 100vw;
  padding: 5vw 0;
  background-color: rgb(191, 191, 191);
  background-repeat: no-repeat;
  background-size: 100% 100%;
  overflow: hidden;

  // 创建模糊遮罩
  &::after {
    content: '';
    width: 100%;
    height: 100%;
    position: absolute;
    left: 0;
    top: 0;
    background: inherit;
    filter: blur(2.5vw);
    z-index: 2;
  }

  // 歌单主体
  .playlistMain {
    position: relative;
    width: 100%;
    display: flex;
    z-index: 3;
  }

  // 歌单封面
  .playlistImg {
    position: relative;
    width: 30vw;
    height: 30vw;
    margin: 0 20px;

    img {
      width: 100%;
      height: 100%;
      background-color: rgb(226, 226, 226);
    }

    span {
      position: absolute;
      left: 0;
      top: 2.5vw;
      height: 4.5vw;
      line-height: 4.5vw;
      padding: 0 1.5vw;
      font-size: 3vw;
      color: #fff;
      border-radius: 0 2.25vw 2.25vw 0;
      background-color: rgba(217, 48, 48, 0.8);
    }

    &::after {
      content: '';
      display: block;
      position: absolute;
      top: 0;
      width: 100%;
      height: 4vw;
      background-image: linear-gradient(90deg, rgba(0, 0, 0, 0), rgba(0, 0, 0, 0.2));
    }

    i {
      position: absolute;
      right: 0;
      text-align: right;
      font-size: 2.3vw;
      color: #fff;
      line-height: 4vw;
      margin-right: 1vw;

      &::after {
        content: '';
        display: block;
        width: 2.2vw;
        height: 2.2vw;
        position: absolute;
        left: 0;
        top: 50%;
        transform: translate(-120%, -50%);
        background-image: url();
        background-repeat: no-repeat;
      }
    }
  }

  // 歌单名称和创作者
  .playlistInfo {
    flex: 1;
    margin-right: 2.5vw;

    h3 {
      font-size: 4vw;
      color: #fff;
      margin-bottom: 2.5vw;
    }

    .playlistInfoAuthor {
      display: flex;

      .playlistInfoImg {
        width: 8vw;
        height: 8vw;
        margin-right: 2vw;
        position: relative;

        img {
          width: 100%;
          height: 100%;
          border-radius: 50%;
          overflow: hidden;
        }

        i {
          position: absolute;
          bottom: 0px;
          right: 0px;
          transform: translateX(50%);
          width: 2.5vw;
          height: 2.5vw;
          background-color: yellow;
          border-radius: 50%;
        }
      }
      span {
        font-size: 3vw;
        line-height: 8vw;
        color: hsla(0, 0%, 100%, 0.7);
      }
    }
  }
}

// 歌曲列表的描述
.playlistSongsTitle {
  height: 4vw;
  font-size: 2vw;
  line-height: 4vw;
  background-color: #eeeff0;
  color: #666;
  padding: 0 1.5vw;
}

// 歌曲列表
.playlistSongs {
  // 歌曲列表的每一项
  .playlistSongsItem {
    padding: 1.5vw 1vw;
    display: flex;
    border-bottom: 1px solid rgba(0, 0, 0, 0.1);

    &:last-child {
      border-bottom: none;
    }

    // 歌曲列表序列
    .playlistSongsItemSerial {
      width: 10vw;
      margin-right: 1vw;
      font-size: 3vw;
      display: flex;
      justify-content: center;
      align-items: center;
      color: #999;
    }

    // 歌曲信息
    .playlistSongsItemMusic {
      // 这里使用 flex:1; 的话就不能显示在一行了
      width: calc(100% - 21vw);

      .musicName {
        font-size: 3vw;
        color: #333;
      }
      .musicSinger {
        font-size: 2.5vw;
        color: #888;
        margin-top: 1vw;
      }
    }

    // 播放按钮
    .play {
      width: 9vw;
      margin-left: 1vw;
      display: flex;
      justify-content: center;
      align-items: center;
      color: #999;

      i {
        display: block;
        width: 6vw;
        height: 6vw;
        background-image: url(https://s3.music.126.net/mobile-new/img/index_icon_2x.png?5207a28c3767992ca4bb6d4887c74880=);
        background-repeat: no-repeat;
        background-size: 40vw;
        background-position: -5vw 0;
      }
    }
  }
}

// 查看更多
.viewMore {
  width: 100vw;
  height: 8vw;
  line-height: 8vw;
  background-color: #eeeff0;
  text-align: center;
  color: #333;

  span {
    display: block;
    width: 100%;
    height: 100%;
    font-size: 2.5vw;
  }
}
</style>
